﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace zykControl
{
    public class ZykComboBox : ComboBox
    {
        public ZykComboBox()
        {
            KeyDown += EditBox_KeyDown;
            KeyPress += EditBox_KeyPress;
        }

        private bool mEnterKeyTab = true;
        /// <summary>
        /// 当在文本框中回车时发生
        /// </summary>
        /// <remarks></remarks>
        [Description("当在文本框中回车时发生")]
        public event KeyEnterEventHandler? KeyEnter;

        public delegate void KeyEnterEventHandler(object sender, EventArgs e);

        [Category("EnterKeyTab")]
        [Description("指示在此文本框中按Enter键是否向下移动焦点")]
        public bool EnterKeyTab
        {
            get
            {
                return mEnterKeyTab;
            }

            set
            {
                mEnterKeyTab = value;
            }
        }

        private void EditBox_KeyDown(object? sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            KeyEnter?.Invoke(this, e);
            if (mEnterKeyTab == true) SendKeys.Send("\t");
        }

        private void EditBox_KeyPress(object? sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r') e.Handled = true;
        }
    }
}